package nrrg.zhujuxing.application.learnscala

import scala.collection.mutable

object mapTest {
  def main(args: Array[String]): Unit = {
    val map1: Map[String, Int] = Map("a" -> 1, "b" -> 2)
    val map2: Map[String, Int] = Map("a" -> 2, "b" -> 1, "c" -> 3)

    println(map1 ++ map2)

    val map3: mutable.Map[String, Int] = mutable.Map("a" -> 2, "b" -> 1, "c" -> 3)

    val map4:mutable.Map[String, Int] = map1.foldLeft(map3)(
      (result:mutable.Map[String, Int], kv: (String, Int)) => {
        val key: String = kv._1
        val value: Int = kv._2
        result(key) = result.getOrElse(key, 0) + value
        result
      }
    )

    println(map4)
  }

}
